package com.ruoyi.web.cp.service.merge;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.web.core.config.ConfigLoader;
import com.ruoyi.web.cp.model.Game;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static com.ruoyi.web.cp.service.merge.ZbConstant.*;

@Slf4j
@Service
public class MergeAqobService {
    @Autowired
    private ConfigLoader configLoader;

    public void mergeAqob() {
        log.info("======> 合并aqob开始");
        long beginTime = System.currentTimeMillis();

        String aqGamePath = configLoader.getRmPath() + "aq-gameScore.txt";

        String obGamePath = configLoader.getRmPath() + "ob-gameScore.txt";
        String obAccurateGamePath = configLoader.getRmPath() + "ob-gameScore-accurate.txt";

        List<Game> agGames = Game.getGames(aqGamePath, "");
        List<Game> agGames2 = Game.getGames(aqGamePath, "");

        List<Game> obGames = Game.getGames(obGamePath, "aqob");
        List<Game> obAccurateGames = Game.getGames(obAccurateGamePath, "aqob");

        Game.calc(agGames, obGames, null, "rm");
        Game.calc(agGames2, obAccurateGames, "accurate", "rm");

        writeMergeResult(agGames, configLoader.getRmPath(), "aqob-merge");
        writeMergeResult(agGames2, configLoader.getRmPath(), "aqob-merge-accurate");

        log.info(StrUtil.format("======> 合并aqob结束,耗时:{}", (System.currentTimeMillis() - beginTime)));
    }

    private void writeMergeResult(List<Game> games, String path, String name) {
        List<String> allGames = new ArrayList<>();
        List<String> fitGames = new ArrayList<>();
        List<String> fitGames30 = new ArrayList<>();
        List<String> fitGames150 = new ArrayList<>();
        List<String> fitGames100 = new ArrayList<>();
        List<String> noMatch = new ArrayList<>(); //未匹配

        for (Game game : games) {
            try {
                StringBuilder sb = new StringBuilder();
                sb.append(game.getDateStr()).append("\t");
                sb.append(game.getLeague()).append("\t");
                sb.append(game.getTeamA()).append("\t");
                sb.append(game.getTeamB()).append("\t");
                sb.append(game.getGameId());
                if (game.getMemo() != null) {
                    sb.append("\t").append(game.getMemo());
                    noMatch.add(sb.toString());
                }
                allGames.add(sb.toString());
                List<String> lists = new ArrayList<>();
                List<String> lists30 = new ArrayList<>();
                List<String> lists150 = new ArrayList<>();
                List<String> lists100 = new ArrayList<>();

                for (Game.Score score : game.getScores()) {
                    StringBuilder sb1 = new StringBuilder();
                    sb1.append(score.getScore()).append("\t");
                    sb1.append(score.getPlv()).append("\t");
                    if (score.getPlv2() != null) {
                        sb1.append(score.getPlv2()).append("\t");
                    }
                    if (score.getPlv3() != null) {
                        sb1.append(score.getPlv3()).append("\t");
                    }
                    if (score.getPlv4() != null) {
                        sb1.append(score.getPlv4()).append("\t");
                    }
                    String fit = score.getFit();
                    String plv = score.getPlv();
                    String plv2 = score.getPlv2();
                    String doublePlv = score.getDoublePlv();
                    String str = sb1.toString();
                    String str2 = sb1.toString();

                    //记录利润>=0
                    if (fit != null && Double.valueOf(fit) > 0) {
                        String s = str + "收：" + fit;
                        lists.add(s.replace("\t", "|"));
                    }
                    if (name.contains("double")) {
                        if (doublePlv != null) {
                            str = str + doublePlv
                                    + "\t" + score.getBdan00Plv()
                                    + "\t" + score.getDrawPlv();
                            str2 = str2 + doublePlv
                                    + "\t" + score.getBdan00Plv()
                                    + "\t" + score.getGoalDesc()
                                    + "\t" + score.getGoalBigPlv()
                                    + "\t" + score.getGoalSmallPlv()
                                    + "\t" + score.getWinPlv()
                                    + "\t" + score.getDrawPlv()
                                    + "\t" + score.getLosePlv()
                                    + "\t" + score.getAsianDesc()
                                    + "\t" + score.getAsianBigPlv()
                                    + "\t" + score.getAsianSmallPlv()
                            ;
                            allGames.add(str);
                        }
                    } else if (name.contains("dy")) {
                        if (doublePlv != null) {
//                            str = str + "\t" + plv3;
                            allGames.add(str);
                        }
                    } else if (name.contains("dx2")) {
                        if (score.getGoalDesc() != null) {
                            str = str + score.getGoalDesc() + "\t" + score.getBdan00Plv();
                            allGames.add(str);
                        }
                    } else {
                        allGames.add(str + fit);
                    }

                    //记录利润>=30
                    if (fit != null && Double.parseDouble(fit) >= MIN_FIT) {
                        String s = str + "收：" + fit;
                        lists30.add(s.replace("\t", "|"));
                    }
                    //记录正波>=150
                    if (plv2 != null && Double.parseDouble(plv2) >= MAX_ZB_PLV_150 && !name.contains("accurate")) {
                        String s = str + "收：" + fit;
                        lists150.add(s.replace("\t", "|"));
                    }
                    //记录正波>=100
                    if (plv2 != null && Double.parseDouble(plv2) >= MAX_ZB_PLV_100 && !name.contains("accurate")) {
                        String s = str + "收：" + fit;
                        lists100.add(s.replace("\t", "|"));
                    }
                    if ("双进".equals(score.getScore())) {
                        String s = "OB双进:" + score.getPlv();
                        if (CollUtil.isNotEmpty(lists100)) {
                            lists100.add(s);
                        }
                        if (CollUtil.isNotEmpty(lists150)) {
                            lists150.add(s);
                        }
                    }
                    if ("大小".equals(score.getScore())) {
                        String s = "OB大小:" + score.getPlv();
                        if (CollUtil.isNotEmpty(lists100)) {
                            lists100.add(s);
                        }
                        if (CollUtil.isNotEmpty(lists150)) {
                            lists150.add(s);
                        }
                    }
                    if ("亚盘".equals(score.getScore())) {
                        String s = "OB亚盘:" + score.getPlv();
                        if (CollUtil.isNotEmpty(lists100)) {
                            lists100.add(s);
                        }
                        if (CollUtil.isNotEmpty(lists150)) {
                            lists150.add(s);
                        }
                    }
                }
                if (CollUtil.isNotEmpty(lists)) {
                    fitGames.add(sb.toString().replace("\t", ","));
                    fitGames.addAll(lists);
                }
                if (CollUtil.isNotEmpty(lists30)) {
                    fitGames30.add(sb.toString().replace("\t", ","));
                    fitGames30.addAll(lists30);
                }
                if (CollUtil.isNotEmpty(lists150)) {
                    fitGames150.add(sb.toString().replace("\t", ","));
                    fitGames150.addAll(lists150);
                }
                if (CollUtil.isNotEmpty(lists100)) {
                    fitGames100.add(sb.toString().replace("\t", ","));
                    fitGames100.addAll(lists100);
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
        FileUtil.writeLines(allGames, path + name + ".txt", "utf-8");
        FileUtil.writeLines(fitGames, path + name + "2.txt", "utf-8");
        FileUtil.writeLines(fitGames30, path + name + "30.txt", "utf-8");
        FileUtil.writeLines(fitGames150, path + name + "150.txt", "utf-8");
        FileUtil.writeLines(fitGames100, path + name + "100.txt", "utf-8");
        FileUtil.writeLines(noMatch, path + "merge" + System.getProperty("file.separator") + "aqob-match" + ".txt", "utf-8");
    }

    public Set<String> getMergeGameNames(String path) {
        List<String> strs = FileUtil.readLines(path, "utf-8");
        Set<String> set = new HashSet<>();
        for (String str : strs) {
            String[] split = str.split(",");
            if (split.length >= 3) {
                set.add(split[2].split("vs")[0].trim());
            }
        }
        return set;
    }
}
